﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelnetRouterApplication.Control;

namespace TelnetRouterApplication.Model
{
    public class Interface
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _ip;

        public string Ip
        {
            get { return _ip; }
            set { _ip = value; }
        }
        private string _mask;

        public string Mask
        {
            get { return _mask; }
            set { _mask = value; }
        }
        private bool _shutdown;

        public bool Shutdown
        {
            get { return _shutdown; }
            set { _shutdown = value; }
        }
        private string _connectionType;

        public string ConnectionType
        {
            get { return _connectionType; }
            set { _connectionType = value; }
        }

        private bool _protocol;
        public bool Protocol
        {
            get { return _protocol; }
            set { _protocol = value; }
        }

        private bool _wan;
        public bool Wan
        {
            get { return _wan; }
            set { _wan = value; }
        }

        private bool _master;
        public bool Master
        {
            get { return _master; }
            set { _master = value; }
        }

        private string _mac;
        public string Mac
        {
            get { return _mac; }
            set { _mac = value; }
        }

        private int _clockrate;
        public int Clockrate
        {
            get { return _clockrate; }
            set { _clockrate = value; }
        }

        public Interface() : this("", "", "", false, "") { }

        public Interface(string name, string ip, string mask, bool shutdown, string connectionType)
        {
            this.Name = name;
            //Defino se é LAN ou WAN pelo nome, se for do tipo Serial é WAN, se não, é LAN
            Wan = false;
            if (!Name.Equals("") && (Name.ToCharArray()[0] == 'S' || Name.ToCharArray()[0] == 's'))
            {
                Wan = true;
            }
            Master = false;
            Mac = "";
            Clockrate = 0;
            this.Mask = mask;
            this.Ip = ip;
            this.Shutdown = shutdown;
            this.ConnectionType = connectionType;
        }

        public static List<Interface> GetListInterface(List<string> messages)
        {
            List<Interface> listInterface = new List<Interface>();

            if (messages.Count > 1)
            {
                //Ignora a primeira e a segunda mensagem porque são de cabeçalho
                for (int i = 2; i < messages.Count; i++)
                {
                    if (!messages[i].Contains('#') && !messages[i].Contains("More"))
                    {
                        string message = messages[i].Replace("\n", "");
                        string[] parameters = message.Split(' ');
                        Interface inter = new Interface();

                        string interfaceName = parameters[0];

                        string interfaceIp = "unassigned";

                        bool ipFound = false;
                        bool statusFound = false;
                        bool protocolFound = false;

                        //Para ignorar os campos de "OK?" e "Method"
                        int ignorer = 0;

                        bool interfaceStatus = false;
                        bool interfaceProtocol = false;

                        for (int j = 1; j < parameters.Length && (!statusFound || !ipFound || !protocolFound); j++)
                        {
                            if (!parameters[j].Equals("") && !parameters[j].Contains("More"))
                            {
                                //Se achou texto e ainda não achou o IP, então é esse
                                if (!ipFound)
                                {
                                    ipFound = true;
                                    interfaceIp = parameters[j];
                                    if (CommandHandler.DEBUG)
                                    {
                                        System.Diagnostics.Debug.WriteLine("IP: " + interfaceIp);
                                    }
                                }
                                //Se não achou o status ainda e já pulou o "OK?" e o "METHOD"
                                else if (!statusFound && ignorer == 2)
                                {
                                    statusFound = true;
                                    if (parameters[j].Equals("up"))
                                    {
                                        interfaceStatus = true;
                                    }
                                }
                                else if (!protocolFound && ignorer == 2)
                                {
                                    protocolFound = true;
                                    if (parameters[j].Equals("up"))
                                    {
                                        interfaceProtocol = true;
                                    }
                                }
                                else
                                {
                                    ignorer++;
                                }
                            }
                        }

                        inter.Name = interfaceName;
                        if (!inter.Name.Equals("") && (inter.Name.ToCharArray()[0] == 'S' || inter.Name.ToCharArray()[0] == 's'))
                        {
                            inter.Wan = true;
                        }
                        inter.Ip = interfaceIp;
                        inter.Shutdown = interfaceStatus;
                        inter.Protocol = interfaceProtocol;

                        listInterface.Add(inter);
                    }
                }
            }

            return listInterface;
        }

        public override bool Equals(object obj)
        {
            if (obj is Interface)
            {
                Interface compared = (Interface) obj;
                bool equals = compared.Name.Equals(this.Name) && compared.Shutdown == this.Shutdown && compared.Ip.Equals(this.Ip) && compared.Mask.Equals(this.Mask);

                return equals;
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            //Para saber se está ligado
            //bool turnedOn = Shutdown && Protocol;
            //string turnedOnString = "off";
            //if (turnedOn)
            //{
            //    turnedOnString = "on";
            //}
            return Name;// + " <" + Ip + "> Status: " + turnedOnString;
        }

        public bool IsTurnedON()
        {
            return Shutdown && Protocol;
        }
    }
}
